package com.mall.controller;

import com.github.pagehelper.PageInfo;
import com.mall.consts.MallConst;
import com.mall.form.ShippingForm;
import com.mall.pojo.Shipping;
import com.mall.pojo.User;
import com.mall.service.IShippingService;
import com.mall.vo.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * @Author: 黄建
 * @Date: 2020/1/26 16:56
 */
@RestController
public class ShippingController {

    @Autowired
    private IShippingService shippingService;

    /**
     * 添加购物地址
     * @param shippingForm
     * @param session
     * @return
     */
    @PostMapping("/shippings")
    public ResponseVo add(@Valid @RequestBody ShippingForm shippingForm, HttpSession session) {
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return shippingService.add(user.getId(), shippingForm);
    }

    /**
     * 删除购物地址
     * @param shippingId
     * @param session
     * @return
     */
    @DeleteMapping("/shippings/{shippingId}")
    public ResponseVo delete(@PathVariable Integer shippingId, HttpSession session) {
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return shippingService.delete(user.getId(), shippingId);
    }

    /**
     * 更新购物地址
     * @param shippingForm
     * @param shippingId
     * @param session
     * @return
     */
    @PutMapping("shippings/{shippingId}")
    public ResponseVo update(@RequestBody ShippingForm shippingForm, @PathVariable Integer shippingId, HttpSession session) {
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return shippingService.update(user.getId(), shippingId, shippingForm);
    }

    /**
     * 分页查询购物地址
     * @param pageNum
     * @param pageSize
     * @param session
     * @return
     */
    @GetMapping("/shippings")
    public ResponseVo<PageInfo<Shipping>> list(@RequestParam(defaultValue = "1", required = false, value = "pageNum") Integer pageNum,
                                               @RequestParam(defaultValue = "10", required = false, value = "pageSize") Integer pageSize,
                                               HttpSession session) {
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return shippingService.list(user.getId(),pageNum,pageSize);
    }
}
